
//
// 101118 - AH - Created.
// 101120 - AH - getResource().
// 101124 - AH - (Garage) Made fields not editable, not going to edit here.
// 101127 - AH - Tags not longer hold by Set<String> by resource but Tags 
//               object.
// 101217 - AH - Resources are now of type Resource, not AttributeObject 
//               directly.
// 110604 - AH - (Bybanen) Changes related to that tags are no longer String
//               but SoekratName objects.
// 110605 - AH - (Garage) Changes related to that resource name is now a 
//               SoekrateName.
//

package org.soekrat.gui.swing.resource;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JTextField;

import org.soekrat.engine.SoekratName;
import org.soekrat.engine.mine.Resource;
import org.soekrat.engine.mine.ResourceValue;
import org.soekrat.engine.mine.Tags;

/**
 * <p>
 *   Resource view panel.
 * </p>
 * @author Arne Halvorsen (AH) 
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class ResourcePanel extends JPanel 
{
  private final JTextField m_nameField=new JTextField(15);
  
  private final JTextField m_tagsField=new JTextField(30);
  
  private Resource m_resource=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public ResourcePanel()
  {
    setLayout(new BorderLayout());
    
    m_nameField.setEditable(false);
    m_tagsField.setEditable(false);
    
    add(m_nameField, BorderLayout.NORTH);
    add(m_tagsField, BorderLayout.SOUTH);
  }
  
  /**
   * <p>
   *   Sets
   *   {@link Resource} to view.
   * </p>
   * @param resource {@code Resource}.
   */
  public void setResource(Resource resource)
  {
    if (resource==null)
    {
      throw new NullPointerException("resource");
    }
    
    m_resource=resource;
    
    SoekratName name=resource.getSoekratName();
    if (name==null)
    {
      throw new IllegalArgumentException("resource got no name");
    }
    m_nameField.setText(name.toString());
    
    ResourceValue rv=(ResourceValue)resource.getAttribute("value");
    if (rv!=null)
    {
      Object v=rv.getValue();
      add(ResourceValueComponentFactory.create(v), BorderLayout.CENTER);
    }
    
    //
    
    Tags tags=(Tags)resource.getAttribute("tags");
    if (tags!=null)
    {
      StringBuilder sb=new StringBuilder();
      boolean first=true;
      for (SoekratName tag : tags)
      {
        if (!first) sb.append(", ");
        first=false;
        sb.append(tag);
        m_tagsField.setText(sb.toString());
      }
    }
  }
  
  /**
   * <p>
   *   Gets resource.
   * </p>
   * @return {@link Resource}.
   */
  public Resource getResource(){ return m_resource; }
  
}
